<?php

namespace App\Http\Controllers\Api;

use App\Dealer;
use App\Http\Resources\WinnerResource;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use App\ActivityStatus;

class DealersController extends Controller
{
    public function lottery(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'type' => [
                'required',
                Rule::in([1,2,3])
            ],
            'num'  => 'required|integer'
        ]);
        if ($validator->fails()) {
            return ['error' => '参数错误'];
        }
        $winners = Dealer::where('is_win', 0)->where('is_fake', 0)->where('is_localed', '是')->inRandomOrder()->limit($request->num)->get();
        foreach ($winners as $winner) {
            $winner->lottery($request->type);
        }
        return WinnerResource::collection($winners);
    }

    public function getWinners()
    {
        $winners = Dealer::where('is_win', '!=', 0)->get();
        return WinnerResource::collection($winners);
    }

    public function getMyOrder(Request $request)
    {
        $dealer = $request->userinfo;
        $dealer = Dealer::find($dealer->id);
        $list = [];
        foreach ($GLOBALS['goods'] as $good) {
            $products = [
                'productId' => $good->id,
                'imageUrl' => $good->pic,
                'productName' => $good->name,
                'productSum' => $dealer->getAttributeValue('goods_'.$good->id),
                'price' => $good->price,
                'min' => get_min_from_market_level($dealer->market_level, $good->id),
            ];
            array_push($list, $products);
        }
        return [
            'success' => true,
            'myOrderList' => $list
        ];
    }

    public function submit(Request $request)
    {
        if (!ActivityStatus::first()->status) {
            return [
                'success' => false,
                'activity_status' => false,
                'error' => '活动已关闭'
            ];
        }
        $dealer = $request->userinfo;
        if ($dealer->type == 'manager') {
            return ['error' => '无权使用'];
        }
        $goodsOrders = $request->input('myOrderList');
        $data = [];
        foreach ($goodsOrders as $goodsOrder) {
            $data['goods_'.$goodsOrder['productId']] = $goodsOrder['productSum'];
        }
        unset($dealer->role, $dealer->type);
        $data['is_order'] = 1;
        $dealer->update($data);
        return ['success' => true, 'activity_status' => true];
    }

    public function getOrderByUsernum(Request $request)
    {
        $manager = $request->userinfo;
        if ($manager->type == 'dealer') {
            return ['error' => '无权访问'];
        }
        $dealer = Dealer::where('username', $request->usernum)->first();
        $list = [];
        foreach ($GLOBALS['goods'] as $good) {
            $products = [
                'productId' => $good->id,
                'imageUrl' => $good->pic,
                'productName' => $good->name,
                'productSum' => $dealer->getAttributeValue('goods_'.$good->id),
                'price' => $good->price,
                'min' => get_min_from_market_level($dealer->market_level, $good->id)
            ];
            array_push($list, $products);
        }
        return [
            'success' => true,
            'companyName' => $dealer->region.'/'.$dealer->market.'/'.$dealer->company,
            'goodsCnt' => set_decimal($dealer->getAllGoodsCount()),
            'moneySum' => set_decimal($dealer->getAllMoneyCount()),
            'myOrderList' => $list
        ];
    }
}
